﻿Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Security
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration
Imports Microsoft.SharePoint.Security
Imports System.Security.Permissions
Imports System.IO
Imports System.Collections.ObjectModel
Imports System.Reflection

Namespace Install


    Public MustInherit Class WebConfigModifierReciever
        Inherits SPFeatureReceiver

        ' Fields
        Private m_ListModifications As New List(Of SPWebConfigModification)()

        ' Properties
        Protected MustOverride ReadOnly Property OwnerModif() As String

        ' Methods
        Protected Sub WebConfigModifier()

        End Sub

        Protected MustOverride Sub AddConfigurationToWebConfig(ByVal app As SPWebApplication)

        Protected Sub AddNodeValue(ByVal name As String, ByVal xpath As String, ByVal scriptResource As String)
            Me.m_ListModifications.Add(Me.GetSPWebModifChild(name, xpath, scriptResource))
        End Sub

        Protected Sub AddOrRemoveHandler(ByVal properties As SPFeatureReceiverProperties, ByVal removeModification As Boolean)
            If properties Is Nothing Then
                Throw New ArgumentNullException("properties")
            End If
            Dim app As SPWebApplication = properties.Feature.Parent
            If app Is Nothing Then
                Dim site As SPSite = properties.Feature.Parent
                If site Is Nothing Then
                    Dim web As SPWeb = properties.Feature.Parent
                    If Not web Is Nothing Then
                        app = web.Site.WebApplication
                    End If
                Else
                    app = site.WebApplication
                End If
            End If

            If app Is Nothing Then
                Throw New ArgumentOutOfRangeException(properties.Feature.Parent.ToString())
            End If

            If Not removeModification Then
                Me.AddConfigurationToWebConfig(app)
            Else
                Me.RemoveWebConfigEntries(app, Me.OwnerModif)
            End If
            app.Update()
        End Sub

        Protected Sub AddSection(ByVal name As String, ByVal xpath As String)
            Me.m_ListModifications.Add(Me.GetSPWebModifSection(name, xpath))
        End Sub

        Protected Sub DoTrace(ByVal Message As String)
            'TODO: Figure out trace
            'TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.High, Guid.Empty, Assembly.GetExecutingAssembly().FullName, Me.OwnerModif, Me.OwnerModif + ".Config", Message)
        End Sub

        <SharePointPermission(SecurityAction.LinkDemand, ObjectModel:=True)> _
    Public Overrides Sub FeatureActivated(ByVal properties As Microsoft.SharePoint.SPFeatureReceiverProperties)
            Dim removeModification As Boolean = False
            Me.AddOrRemoveHandler(properties, removeModification)
        End Sub

        <SharePointPermission(SecurityAction.LinkDemand, ObjectModel:=True)> _
        Public Overrides Sub FeatureDeactivating(ByVal properties As Microsoft.SharePoint.SPFeatureReceiverProperties)
            Dim removeModification As Boolean = True
            Me.AddOrRemoveHandler(properties, removeModification)
        End Sub

        <SharePointPermission(SecurityAction.LinkDemand, ObjectModel:=True)> _
        Public Overrides Sub FeatureInstalled(ByVal properties As Microsoft.SharePoint.SPFeatureReceiverProperties)

        End Sub

        <SharePointPermission(SecurityAction.LinkDemand, ObjectModel:=True)> _
        Public Overrides Sub FeatureUninstalling(ByVal properties As Microsoft.SharePoint.SPFeatureReceiverProperties)

        End Sub

        Protected Function GetSPWebModifAttribut(ByVal name As String, ByVal xpath As String, ByVal value As String) As SPWebConfigModification
            Dim modification As New SPWebConfigModification(name, xpath)
            modification.Owner = Me.OwnerModif
            modification.Sequence = 0
            modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute
            modification.Value = value
            Return modification
        End Function


        Protected Function GetSPWebModifChild(ByVal name As String, ByVal xpath As String, ByVal scriptResource As String) As SPWebConfigModification
            Dim modification As New SPWebConfigModification(name, xpath)
            modification.Owner = Me.OwnerModif
            modification.Sequence = 0
            modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode
            modification.Value = scriptResource
            Return modification
        End Function

        Protected Function GetSPWebModifSection(ByVal name As String, ByVal xpath As String) As SPWebConfigModification
            Dim modification As New SPWebConfigModification(name, xpath)
            modification.Owner = Me.OwnerModif
            modification.Sequence = 0
            modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection
            Return modification
        End Function

        Protected Sub RemoveWebConfigEntries(ByVal oWebApp As SPWebApplication, ByVal owner As String)
            Dim webConfigModifications As Collection(Of SPWebConfigModification) = oWebApp.WebConfigModifications
            Dim num As Integer = webConfigModifications.Count
            For i As Integer = num - 1 To 0 Step -1
                Dim modification As SPWebConfigModification = webConfigModifications(i)
                If modification.Owner Is owner Then
                    webConfigModifications.Remove(modification)
                End If
            Next

            If num > webConfigModifications.Count Then
                oWebApp.Update()
                SPFarm.Local.Services.GetValue(Of SPWebService)().ApplyWebConfigModifications()
            End If

        End Sub


        Protected Sub SaveWebConfig(ByVal application As SPWebApplication)
            For Each m As SPWebConfigModification In Me.m_ListModifications
                application.WebConfigModifications.Add(m)
            Next
            Me.m_ListModifications.Clear()
            SPFarm.Local.Services.GetValue(Of SPWebService)().ApplyWebConfigModifications()
        End Sub

    End Class

End Namespace